﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using  BusinessHandler;
using EasyCode.Utility;
using BusinessHanler;

public partial class Page_CommentManagement : System.Web.UI.Page
{
    private int currentPage=1;
    private int pageSize=20;
    private NewsCommentController newsCommentController;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            this.GetList();
        }
    }
    private void GetList()
    {
        NewsCommentController.Instance=null;
        newsCommentController=NewsCommentController.Instance;
        NewsComment condition=new NewsComment();
        DateTime date;
        if (!string.IsNullOrEmpty(this.TextBox_Title.Text))
        {
            condition.CommentTitle=HttpUtility.HtmlDecode(this.TextBox_Title.Text);
        }
        if (this.DropDownList_Status.SelectedValue!=string.Empty)
        {
            condition.Status=DropDownList_Status.SelectedValue=="1"?true:false;
        }
        if (DateTime.TryParse(this.TextBox_StartDate.Text, out date))
        {
            condition.CreatedDate=date;
        }
        int skip=(currentPage-1)*pageSize;
        int total=0;
        var list=(from item in newsCommentController.NewsComments
                  where (condition.CommentTitle==null||item.CommentTitle.Contains(condition.CommentTitle))&&
                        (condition.CommentTitle==null||item.CommentDetail.Contains(condition.CommentTitle))&&
                        (condition.Status==null||item.Status==condition.Status)&&
                        (condition.CreatedDate==null||item.CreatedDate==date)
                  orderby item.CommentTitleID descending
                  select item);
        total=list.Count();
        List<NewsComment> commentList=list.Skip(skip).Take(pageSize).ToList();
        if (commentList!=null&&total>0)
        {
            tr_not_found.Visible=false;
        }
        else
        {
            tr_not_found.Visible=true;
        }
        Repeater_Content.DataSource=commentList;
        Repeater_Content.DataBind();
        this.BuildPageList(total);

    }
    protected void Button_Search_Click(object sender, EventArgs e)
    {
        this.GetList();
    }
    protected void Button_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("CommentManagement.aspx");
    }

    protected void UpdatePanelContent_Load(object sender, EventArgs e)
    {
        string arg=Request.Form["__EVENTARGUMENT"];
        if (string.IsNullOrEmpty(arg)) return;
        if (arg!=null)
        {
            string[] temp=arg.Split('|');
            if (temp.Length==2)
            {
                if (temp[1].ToString().ToUpper()=="PAGE")
                {
                    this.currentPage=Protector.Int(temp[0].ToString(), 0);
                    this.GetList();
                }
            }
        }
    }
    protected void Repeater_Content_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id=Protector.Int(e.CommandArgument, 0);
        NewsCommentController.Instance=null;
        newsCommentController=NewsCommentController.Instance;
        var c=(from item in newsCommentController.NewsComments where item.CommentTitleID==id select item).SingleOrDefault();
        if (c!=null)
        {
            if (e.CommandName=="LockUnLockCommand")
            {
                if (!CheckRoles.CanEdit())
                {
                    Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
                }

                if (c.Status==null)
                {
                    c.Status=true;
                }
                else
                {
                    c.Status=!c.Status;
                }
                try
                {
                    newsCommentController.SubmitChanges();
                    Message_Alert.SetSuccess(Resources.Comment.ChangeStatusSuccess);
                    GetList();
                }
                catch (Exception ex)
                {
                    Message_Alert.SetError(Resources.Comment.ChangeStatusFail+"Error: "+ex.Message);
                }
            }
            else if (e.CommandName=="DeleteCommand")
            {
                if (!CheckRoles.CanDelete())
                {
                    Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
                }
                try
                {
                    newsCommentController.NewsComments.DeleteOnSubmit(c);
                    newsCommentController.SubmitChanges();
                    Message_Alert.SetSuccess(Resources.Comment.DeletedSuccess);
                    GetList();
                }
                catch
                {
                    Message_Alert.SetError(Resources.Comment.DeleteFail);
                }
            }
        }
    }

    private void BuildPageList(int numOfRecord)
    {
        string pagingText="";
        int numOfPage=(numOfRecord/pageSize);
        if ((numOfRecord%pageSize)>0)
        {
            numOfPage=numOfPage+1;
        }
        if (numOfPage>1)
        {
            string pagingLink=Commons.LinkPageForAdmin(numOfPage, this.currentPage, 11, 3);
            pagingText="<div class='paginator2 right'>"+pagingLink+"</div>";
        }

        Literal_PagingListBot.Text=Literal_PagingListTop.Text=pagingText;
    }
}